import { ref, Ref } from 'vue';
import { useBasicModel } from './useBasicModel';

/** 使用地图 */
export function useScene(modelid: Ref<string | null>, viewTokenApi: Function) {
  const map = ref();

  const { elementid, viewer3D, model3D, drawable } = useBasicModel(modelid, viewTokenApi);

  // 初始化地图
  function initMap({ longitude, latitude, altitude, rotation }) {
    const mapConfig = new window.Glodon.Bimface.Plugins.TileMap.MapConfig();
    mapConfig.viewer = viewer3D.value;
    // 设置模型载入的基点，即模型上某一特征点的世界坐标，可通过鼠标点击事件获取
    mapConfig.basePoint = {
      x: 0,
      y: 0,
    };
    // 设置模型载入基点所对应的经纬度（WGS84）
    mapConfig.modelPosition = [longitude || 121.62844633392, latitude || 29.840569498277];
    // 设置模型的旋转弧度值
    mapConfig.modelRotationZ = rotation || (0 * Math.PI) / 180;
    // 设置模型零零标高对应的高程值，单位为米
    mapConfig.modelAltitude = altitude || 0;
    // 构造地图对象
    map.value = new window.Glodon.Bimface.Plugins.TileMap.Map(mapConfig);
    // 设置地图类型为天地图的影像底图，天地图key可在http://lbs.tianditu.gov.cn/server/MapService.html申请
    map.value.setMapSource({
      url: 'http://t0.tianditu.gov.cn/img_w/wmts?tk=9ed11478db7734b5caf75cc1775f0118',
      provider: 'Tianditu',
      credit: window.Glodon.Bimface.Common.Credit.Tianditu,
    });
  }

  // 销毁地图
  function destroyMap() {
    if (!map.value) {
      return;
    }
    map.value.destroy();
    viewer3D.value.render();
  }

  function hideToolbar(hide: boolean) {
    const toolbarList: NodeListOf<HTMLElement> = document.querySelectorAll('.bf-toolbar');
    const modelTree: HTMLElement | null = document.querySelector('.bf-modelTree-panel');
    if (toolbarList.length) {
      toolbarList.forEach((toolbar) => {
        hide ? (toolbar.style.display = 'none') : (toolbar.style.display = '');
      });
    }
    if (modelTree) {
      modelTree.style.display = 'none';
    }
  }

  function restoreDefault() {
    hideToolbar(false);
    model3D.value.restoreAllDefault();
    model3D.value.clearSelectedComponents();
    model3D.value.opaqueAllComponents();
    model3D.value.clearIsolation();
    model3D.value.clearOverrideColorComponents();
    viewer3D.value.render();
  }

  function overrideComponents(ids, color) {
    const componentColor = new window.Glodon.Web.Graphics.Color(color, 0.8);
    model3D.value.overrideComponentsColorById(ids, componentColor);
    viewer3D.value.render();
  }

  /**
   * 定位到构件
   * @param ids 构件id列表
   */
  function zoomToComponents(ids: Array<string>) {
    model3D.value.setSelectedComponentsById(ids);
    model3D.value.zoomToSelectedComponents();
    model3D.value.clearSelectedComponents();
  }

  /**
   * 隔离构件
   * @param ids 构件id列表
   */
  function isolateComponentsById(ids) {
    const makeOthersTranslucent = window.Glodon.Bimface.Viewer.IsolateOption.MakeOthersTranslucent;
    model3D.value.isolateComponentsById(ids, makeOthersTranslucent);
    viewer3D.value.render();
  }

  return {
    elementid,
    viewer3D,
    model3D,
    map,
    drawable,
    initMap,
    destroyMap,
    hideToolbar,
    restoreDefault,
    overrideComponents,
    zoomToComponents,
    isolateComponentsById,
  };
}
